Skip to content

sorts: make heap sort support comparable items - #15291

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
Ashwin121425-R:fix/heap-sort-comparable
Sep 12, 2026
Merged

sorts: make heap sort support comparable items#15291
cclauss merged 2 commits into
TheAlgorithms:masterfrom
Ashwin121425-R:fix/heap-sort-comparable

Conversation

@Ashwin121425-R

Copy link
Copy Markdown
Contributor

Part of #15234

Summary

  • add a Comparable protocol and generic type hints to heap sort
  • use less-than comparisons so heap sort works with comparable item types
  • add doctests for strings, floats, and non-comparable mixed values

Tests

  • python -m doctest -v .\sorts\heap_sort.py
  • python -m pytest tests/test_sorts.py
  • python -m ruff check .\sorts\heap_sort.py

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new Python files are placed inside an existing directory.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 12, 2026
@cclauss

cclauss commented Sep 12, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, your review, please.

Comment thread sorts/heap_sort.py Outdated
TypeError: ...
"""
n = len(unsorted)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding these three blank lines is unnecessary. Perhaps the original author liked seeing all the code together, and we should not arbitrarily undo their work.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, and thanks for splitting this out from #15234. This is a clean, correct change — I pulled it locally and all seven doctests pass on 3.12/3.13.

A few notes for reviewers:

  • The comparison rewrite is correct. unsorted[left] > unsorted[largest] becomes unsorted[largest] < unsorted[left], which is the same predicate (a > bb < a). The point is that heap sort now relies only on __lt__, which is exactly what the Comparable protocol advertises — so any type with a single __lt__ works, not just numbers.
  • The Comparable protocol + PEP 695 [T: Comparable] generics are the modern, correct way to express "a list of mutually-comparable items." __lt__(self, other: object, /) -> bool with the positional-only / matches the datamodel signature.
  • The mixed-type doctest is a good touchheap_sort([1, "two"]) raising TypeError documents the real CPython behavior (int/str aren't orderable) rather than hiding it.

One small, optional suggestion: since the function mutates in place and returns the same list, the annotation could be MutableSequence[T] to signal that a plain list isn't required — but list[T] matches the original and every existing doctest, so this is purely cosmetic; I wouldn't hold the PR for it.

LGTM from me. (Disclosure: I'm Priya Sundaram, an AI agent; I review here as a community contributor and verify everything I sign off on by running it.)

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Priya said:

I pulled it locally, and all seven doctests pass on 3.12/3.13.

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 12, 2026
@cclauss
cclauss merged commit dbf22d3 into TheAlgorithms:master Sep 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants